<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

JFX::checkAdminPermsRedirect('core', 'perm_edit_page');

$THEME->addBreadcrumb($this->adminUrl.'/edit-page/', $this->lang('ap_title_edit-page'));

$VIEW->template = 'admin-edit-page.tpl';

$VIEW->addJsLink($CONFIG->baseUrl.'/jfx/jfx/page-width-slider.js');



if((int) get('page') <= 0){
    $pageid = $DB->oneValue($CONFIG->dbprefix.'pages', 'id', 'is_default = 1');
    JFX::redirect($CONFIG->adminUrl.'/edit-page/?page='.$pageid);
}else{
    $pageid = (int) get('page');
    if($DB->countRows($CONFIG->dbprefix.'pages', "id = '{$pageid}'")==0){
        $pageid = $DB->oneValue($CONFIG->dbprefix.'pages', 'id', 'is_default = 1');
    }
}

if($pageid == $SETTINGS->get('template_page_id')){
    JFX::addError('You cannot edit the templates page. Use template management in the left column.');
    JFX::redirect($CONFIG->adminUrl);
}

$SMARTY->assign('currentPageId', $pageid);
$SMARTY->assign('currentPageTitle', $LANG->getContent('core', 'page_title', $pageid));

JFX::checkAdminPermsRedirect('core', 'perm_page_'.$USER->workspace.'_'.$pageid);


$PAGE = new JFX_Page;
$PAGE->loadById($pageid);
$SMARTY->assign('PAGE', $PAGE->get());

$pageButtons = $THEME->getPageButtons($pageid);
$SMARTY->assign('pageButtons', $pageButtons);

echo '<div class="jfx-droppable jfx-page" id="jfxpage-'.$pageid.'"></div>';

$templates = array();
$block = new JFX_Block;
$tps = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}blocks WHERE page_id = '{$pageid}' AND is_template = 1 AND workspace_id = '{$USER->workspace}'");
foreach($tps as $k=>$v){
    $block->loadById($v['id'], $v['workspace_id'], false, true);
    $details = $block->getDetails();
    $tps[$k]['title'] = $this->getContentLang('block_title', $block->id, '', $block->workspaceid);
}


// sort by title
$newtps = array();
foreach($tps as $v){
    $newtps[$v['title']] = $v;
}

ksort($newtps);
foreach($newtps as $v){
    $templates[] = array(
        'title' => $v['title'],
        'id' => $v['id']
    );
}


$modules = array();
$mods = JFX_Module::getInstalledModules();
$count = 0;
foreach($mods as $k=>$v){
    $module = JFX::registry('JFX_Module_'.$v['keyname']);
    $actions = $module->getBlockActions();
    if(!is_array($actions) || count($actions)==0) continue;
    $modules[$count] = array(
        'keyname' => $module->keyname,
        'title' => $module->getTitle(),
        'actions' => array()
    );
    foreach($actions as $key=>$value){
        $modules[$count]['actions'][] = array(
            'title' => $value['title'],
            'keyname' => $value['keyname']
            //'description' => trim((string) $value['description'])
        );
    }
    $count++;
}


$BLOCK = new JFX_Block;
$insertMenu = '<ul class="insert-menu">
<div id="insert-menu-close-button"><img src="'.$CONFIG->imageUrl.'/admin/layout/close-normal.png" alt="Close" /></div>
<li class="templates">Templates
<ul>';
foreach($templates as $k=>$v){
    $BLOCK->loadById($v['id']);
//    $blockDetails = str_replace(array("'", '"'), array("\'", "'"), json_encode($BLOCK->getDetails()));
    $blockDetails = $BLOCK->getDetailsRecursive();
    //var_dump($blockDetails); exit;
    $insertMenu .= '<li class="template">
    <a href="javascript: void(null)" onclick="AdminBlocks.addBlock(AdminBlocks.elementForInserting, '.str_replace(array("'", '"'), array("\'", "'"), json_encode($blockDetails)).');">'
    .$v['title'].'</a></li>';

}

$insertMenu .= '</ul>
</li>
<li class="modules">Modules
<ul>';

foreach($modules as $k=>$module){
    $insertMenu .= '<li class="module '.$module['keyname'].'"><h3 class="title">'.$module['title'].'</h3>
<ul>';
    foreach($module['actions'] as $k2=>$action){
        $BLOCK->newModuleBlock($module['keyname'], $action['keyname']);
        $blockDetails = str_replace(array("'", '"'), array("\'", "'"), json_encode($BLOCK->getDetails()));

        $insertMenu .= '<li class="action '.$action['keyname'].'">
            <a href="javascript: void(null)" id="jfxinsertmenu-'.$module['keyname'].'-'.$action['keyname'].'"
                onclick="AdminBlocks.addBlock(AdminBlocks.elementForInserting, '.$blockDetails.');">'.
                $action['title'].'</a></li>';
    }
$insertMenu .= '</ul>
</li>';
}

$insertMenu .= '</ul>
</li>
</ul>';









$SMARTY->assign('insertMenu', $insertMenu);






$SMARTY->assign('canEditPageDetails', true);
$SMARTY->assign('showPageWidthSlider', true);

JFX::hook('JFX_Core.editPageLoad', $pageid);







